/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.MultiplicityElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;


/**
 * The class is used to display and modify the multiplicities
 * of an elment.
 */
public class MultiplicitySection extends PropertyViewSection {


	/**
	 * The combo of multiplicity.
	 */
	private CCombo mutiplicityCombo;
	
	/**
	 * The focus listener.
	 */
	private FocusListener focusListener;

	/**
	 * Creates the controls.
	 * 
	 * @param tabbedPropertySheetPage the tabbed property sheet page
	 * @param parent the parent
	 */
	@Override
	public void createControls(final Composite parent,
			final TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		this.getWidgetFactory().createCLabel(composite, "Multiplicity:"); //$NON-NLS-1$

		this.mutiplicityCombo = this.getWidgetFactory().createCCombo(composite, SWT.NONE);
		this.mutiplicityCombo.setEditable(true);
		data = new FormData(100,15);
		data.left = new FormAttachment(0, AbstractPropertySection.STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		this.mutiplicityCombo.setLayoutData(data);
		this.mutiplicityCombo.add("0..1");
		this.mutiplicityCombo.add("0..*");
		this.mutiplicityCombo.add("1");
		this.mutiplicityCombo.add("1..*");
		this.mutiplicityCombo.add("*");
		
		//add focus listener
		this.focusListener= new FocusListener() {
			public void focusGained(final FocusEvent e){}
			public void focusLost(final FocusEvent e) {
				final String selectedMul = MultiplicitySection.this.mutiplicityCombo.getText();
				MultiplicitySection.this.affectMultiplicity((MultiplicityElement) MultiplicitySection.this.getElement(),
						selectedMul);
			}

		};
		this.mutiplicityCombo.addFocusListener(this.focusListener);
	}

	/**
	 * Affect multiplicity to the element.
	 * 
	 * @param element the element that is a multiplicity element
	 * @param value the value of multiplicity ex. 1..3, 2,* ...
	 */
	public void affectMultiplicity(final MultiplicityElement element,final String value){
		final StringTokenizer mulToken = new StringTokenizer(value, "..");
		String lower = mulToken.nextToken();
		String upper = lower;
		if(mulToken.countTokens()>0) {
			upper = mulToken.nextToken();
		}

		// property multiplicity settings
		if(lower.equals("*")) {
			upper = "-1";
			lower = "0";
		}
		else if(upper.equals("*")) {
			upper="-1";
		}

		if (this.getElement() instanceof MultiplicityElement) {
			final LiteralInteger eltLower= UMLFactory.eINSTANCE.createLiteralInteger();
			eltLower.setValue(Integer.parseInt(lower));
			element.setLowerValue(eltLower);
			
			final LiteralUnlimitedNatural eltUpper= UMLFactory.eINSTANCE.createLiteralUnlimitedNatural();
			eltUpper.setValue(Integer.parseInt(upper));
			element.setUpperValue(eltUpper);
		}


	}	



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * Refresh the view othe multicplicity in the multicplicityCombo.
	 */
	@Override
	public void refresh() {
		if(!this.mutiplicityCombo.isDisposed()){
			this.mutiplicityCombo.removeFocusListener(this.focusListener);
			if(this.getElement() instanceof MultiplicityElement) {
				final String lower = String.valueOf(((MultiplicityElement)this.getElement()).getLower());
				final String upper = String.valueOf(((MultiplicityElement)this.getElement()).getUpper());
				if(upper.equals("-1")) {
					this.mutiplicityCombo.setText(lower+".."+"*");
				} else {
					this.mutiplicityCombo.setText(lower+".."+upper);
				}
			}
			this.mutiplicityCombo.addFocusListener(this.focusListener);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(this.mutiplicityCombo!=null && !this.mutiplicityCombo.isDisposed())
			this.mutiplicityCombo.removeFocusListener(this.focusListener);
	}

}

